// components/picker/address.js
 
var sysdata = require('../../model/sysdata');
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    authorize: false,
    citys: null,
    option: null,
    index: 0,
    scrollTop: 0,
    value: [
      { id: "", name: '请选择' },
      { id: "", name: '请选择' },
      { id: "", name: '请选择' },
    ]
  },
  ready: function () {

  },
  /**
   * 组件的方法列表
   */
  methods: {
    setinitdata(province,city,county) {
      this.setData({
        authorize: true
      })  
      var that= this;
   
      if(!this.data.citys){
        sysdata.citys().then(function(result){
          if(result["status"]==1){
            that.setData({citys:result["obj"],option:result["obj"]})
            that.setselect([province,city,county])
          }
        })
      }
      else{

        that.setselect([province,city,county])
      }
    },

    setselect(nams) {
     
      if(!nams){

        return false
      }
      if (typeof (nams) == "string") {
        var reg = /.+?(省|市|自治区|自治州|县|区)/g;
        nams = nams.match(reg)
      }
      var that = this;
      var options = that.data.citys;

   

      var seletval = [];
      var selectnum = 0;
      for (var i = 0; i < 3; i++) {
        if (nams[i]) {
          var bvalue = false
          for (var a in options) {

            if (options[a].name.indexOf(nams[i]) > -1) {
              seletval.push({ id: a, name: options[a].name })
              bvalue = true
              if (i != 2) {
                options = options[a].sub;
              }
              selectnum++
              break;

            }
          }
          if (!bvalue) {
            break;
          }

        }
      }

      for (var n = selectnum; n < 3; n++) {
        seletval.push({ id: "", name: "请选择" })
      }
      this.setData({
        value: seletval,
        option: options,
        index: selectnum > 2 ? 2 : selectnum,
      })
      if (selectnum == 3) {

        return { status: 1, seletval }
      }
      else {

        return { status: -1, seletval }
      }


    },
    chooseid: function (e) {
      var that = this;
      var dataset = e.currentTarget.dataset;
      var value = that.data.value;
      var index = dataset.index;
      var option = this.getoption(index)
      for (var i = index; i < 3; i++) {
        value[i].id = "";
        value[i].name = "请选择";
      }
      that.setData({
        option, value, index, scrollTop: 0
      })
    },
    del: function () {
      this.setData({
        authorize: !this.data.authorize
      })
    },

    getoption(index) {
    
      if (!index) {
        index = this.data.index
      }
      var value = this.data.value
      var options = this.data.citys;
      for (var n = 0; n < index; n++) {
        options = options[value[n].id].sub;
      }
      return options
    },
    clearselect: function () {
      this.data.index=0

      var option = this.getoption();

      this.setData({
        option, value: [
          { id: "", name: '请选择' },
          { id: "", name: '请选择' },
          { id: "", name: '请选择' },
        ], index: 0, scrollTop: 0
      })
    },
    choose: function (e) {
      var that = this;
      var values = that.data.value;
      var index = that.data.index;
      var option = that.data.option;
      var dataset = e.currentTarget.dataset;
      values[index]['name'] = dataset.name;
      values[index]['id'] = dataset.id;
      if (index < 2) {
        index = index * 1 + 1;
        option = option[dataset.id].sub;
        var authorize = true
      } else {
        var authorize = false
      }
      that.setData({
        value: values,
        option: option,
        index: index,
        scrollTop: 0,
        authorize: authorize
      })
      if (authorize == false) {
        var myEventDetail = values // detail对象，提供给事件监听函数
        var myEventOption = values // 触发事件的选项
     
        that.triggerEvent('chooseaddress', myEventDetail, myEventOption)
      }

    }
  }
})
